/*
 * Created on 2006-2-13
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.util.lang;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hitrust.gs.lib.db.GSBaseDAO;

/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class IDGenerator extends GSBaseDAO{

    /**
     * 已作废
     * @param tableName
     * @param orgId
     * @return
     * @throws Exception
     */
    public long generatorID(String tableName,String orgId,String categoryId) throws Exception {
       Connection conn = null;
       long value = -1;
       try{
           conn = this.getConnetion();
           conn.setAutoCommit(false);
           
           String sql = "select GS_SEQ_CATEGORY_ID from GS_SEQNUM_MANAGE where GS_SEQ_CATEGORY_NAME = ? and GS_ORG_ID = ?";
           PreparedStatement pstmt = conn.prepareStatement(sql);
           pstmt.setString(1,tableName);
           pstmt.setString(1,orgId);
           ResultSet rs = pstmt.executeQuery();
           rs.next();
           
           String categoryid = rs.getString(1);
           
           value = GenerateID(conn,orgId,categoryid,1);
           conn.commit();
       }catch(Exception e){
           try{conn.rollback();}catch(SQLException ee){}
           throw e;
       }finally{
           try{conn.setAutoCommit(true);}catch(SQLException ee){}
           close(conn);
       }
           
       return value;
        
    }
    
    public long generatorID(String orgId,String categoryId) throws Exception {
        long value = -1;
        Connection conn = null;        
        try{
            conn = this.getConnetion();
            conn.setAutoCommit(false);
            
            value = GenerateID(conn,orgId,categoryId,1);
            conn.commit();
        }catch(Exception e){
            try{conn.rollback();}catch(SQLException ee){}
            throw e;
        }finally{
            try{conn.setAutoCommit(true);}catch(SQLException ee){}
            close(conn);
        }
            
        return value;
    }
	private long GenerateID(Connection conn,String orgId,String categoryId,long count) throws Exception{
		java.sql.PreparedStatement pstmt = null;
		
			long value = 0;

			String categoryName = "";
			if (categoryId.equals("01"))
			{
				categoryName = "正常采购";
			}
			else if (categoryId.equals("02"))
			{
				categoryName = "应急采购";
			}
			else if (categoryId.equals("03"))
			{
				categoryName = "退货";
			}			

			String sql = "select GS_CURRENT_SEQ from GS_SEQNUM_MANAGE where GS_ORG_ID=? and GS_SEQ_CATEGORY_ID=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, orgId);
			pstmt.setString(2, categoryId);
			ResultSet rst1 = pstmt.executeQuery();	
			pstmt.clearParameters();
			if (!rst1.next())
			{
				sql = "insert into GS_SEQNUM_MANAGE values (?,?,?,?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, orgId);
				pstmt.setString(2, categoryId);
				pstmt.setString(3, categoryName);
				pstmt.setString(4, "0");	
				pstmt.executeUpdate();
				conn.commit();		
				pstmt.clearParameters();							
			}
			if (rst1 != null) rst1.close();
			
			
			sql = "update GS_SEQNUM_MANAGE set GS_CURRENT_SEQ = GS_CURRENT_SEQ +? " +
					"where GS_CURRENT_SEQ=(" +
					"select GS_CURRENT_SEQ from GS_SEQNUM_MANAGE where GS_ORG_ID=? and GS_SEQ_CATEGORY_ID=?" +
					") and GS_ORG_ID=? and GS_SEQ_CATEGORY_ID=?";
			
			//String sql = "decode(GS_CURRENT_SEQ,99999,0)"
			pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, count);
			pstmt.setString(2,orgId);
			pstmt.setString(3,categoryId);
			pstmt.setString(4,orgId);
			pstmt.setString(5,categoryId);
			pstmt.executeUpdate();
			conn.commit();

			pstmt.clearParameters();

			sql = "select GS_CURRENT_SEQ from GS_SEQNUM_MANAGE where GS_ORG_ID=? and GS_SEQ_CATEGORY_ID=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, orgId);
			pstmt.setString(2, categoryId);
			ResultSet rst = pstmt.executeQuery();
			rst.next();
			value = rst.getLong(1);
            pstmt.close();
			
		
		return value;
	}
	
	/**
	 * 按需要的位数补齐id长度,左补零
	 * @param id 需要补齐长度的id
	 * @param length
	 * @return
	 */
	public String formatID(long id,int length){
	    String buffer = Long.toString(id);
	    int shortLength = length - buffer.length();
	    if(shortLength <= 0)
	        return buffer;
	    StringBuffer sb = new StringBuffer("");
	    for(int i=0;i<shortLength;i++){
	        sb.append("0");
	    }
	    buffer = sb.toString() + buffer;
	    
	    return buffer;
	}
    
}
